package com;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class SpringTemplate {
	
	@Test
	public void reciveMessage() throws JMSException
	{
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		JmsTemplate template =(JmsTemplate) context.getBean("jmsTemplate");
		Queue queue = (Queue)context.getBean("queueDestination");
		TextMessage message = (TextMessage) template.receive(queue);
		System.out.println(message.getText());
	}
	
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		JmsTemplate template =(JmsTemplate) context.getBean("jmsTemplate");
		Queue queue = (Queue)context.getBean("queueDestination");
		template.send(queue, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				// TODO Auto-generated method stub
				TextMessage message = session.createTextMessage("Template test...");
				return message;
			}
		});
		template.send(queue, new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				// TODO Auto-generated method stub
				TextMessage message = session.createTextMessage("Template test...");
				return message;
			}
		});
	
	}
}
